//
//  MusicStoreView.swift
//  GFQM
//
//  Created by iSolar on 2017/4/21.
//  Copyright © 2017年 iSolar. All rights reserved.
//

import UIKit

class MusicStoreView: UIScrollView , CirCleViewDelegate ,UIScrollViewDelegate{
    var circleView: CirCleView!
    var topView: UIView = UIView()
    let KScreenWidth: CGFloat = UIScreen.main.bounds.width
    let KScreenHeight: CGFloat = UIScreen.main.bounds.height
    let imageArray: [UIImage?] = [UIImage(named: "1.png"), UIImage(named: "2.png"), UIImage(named: "3.png")]
    var images = ["img1","img2","img3","img4","img5","img6"]
    var titleArr = ["个性化推荐你喜欢的歌曲","唯美古风 倾心沉醉","萌萌侵略者【快听】","【知性】----成熟女性最好的标志","【安静向】觉得烦躁时不妨听一下","如果这都不算爱 ？？？"]
    //MARK:- 构造函数
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.contentSize = CGSize(width:0,height:KScreenWidth*2)
        self.showsVerticalScrollIndicator = true
        self.showsHorizontalScrollIndicator = false
        self.isScrollEnabled = true // 可以上下滚动
        topView.backgroundColor = UIColor.white
        setupCircleView()
        setupTopView()
        setupContentView()
    }
    
    func setupCircleView() {
        self.circleView = CirCleView(frame: CGRect(x: 0, y: 0, width: KScreenWidth, height: 140), imageArray: imageArray)
        circleView.delegate = self
        addSubview(self.circleView)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //MARK:- Delegate Methods
    //MARK: CirCleViewDelegate Methods
    
    func clickCurrentImage(_ currentIndxe: Int) {
        print(currentIndxe, terminator: "");
    }
    
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        // 上下拉时滚动时的位移量（向上滚动时，位移是正数；向下拉动时，位移是负数）
        let offsetY = scrollView.contentOffset.y
        print("offsetY = \(offsetY)")
        
        // 最大位移量（注意scrollview的contentsize的高与当前视图控制器的屏幕高度的关系）
        let offsetYMax = scrollView.contentSize.height
        print("offsetYMax = \(offsetYMax)")
        let offsetYTmp = offsetYMax - self.frame.size.height
        print("offsetYTmp = \(offsetYTmp)")
        
        if 0 >= offsetY
        {
            // 向下拉
            if -100.0 >= offsetY {
                scrollView.contentInset = UIEdgeInsetsMake(80.0, 0.0, 0.0, 0.0)
            }
        } else {
            // 向上拉
            let offsetYUp = offsetY - offsetYTmp
            if 100.0 <= offsetYUp {
                scrollView.contentInset = UIEdgeInsetsMake(0.0, 0.0, 80.0, 0.0)
            }
        }
    }
    
}

extension MusicStoreView {
    fileprivate func setupTopView() {
        addSubview(topView)
        topView.frame = CGRect(x: 0, y: 140, width: KScreenWidth, height: ((KScreenWidth-60)/5 + 20)*2+10)
        
        var imageArr = NSArray()
        var titleArr = NSArray()
        imageArr = ["签到有礼","排行","电台","歌手","视频","分类","酷我耳机","音乐包","HiFi音乐","DJ"]
        titleArr = ["签到有礼","排行","电台","歌手","视频","分类","酷我耳机","音乐包","HiFi音乐","DJ"]
        var startX: CGFloat = 10
        var startY: CGFloat = 0
        
        for index in 0..<imageArr.count {
            let btn = UIButton()
            btn.frame = CGRect(x: startX, y: startY, width: (KScreenWidth-60)/5, height: (KScreenWidth-60)/5 + 20)
            btn.titleLabel?.font = UIFont.boldSystemFont(ofSize: 13) //文字大小
            btn.setTitleColor(UIColor.lightGray, for: .normal) //文字颜色
            btn.set(image: UIImage(named: imageArr[index] as! String), title: titleArr[index] as! String, titlePosition: .bottom,
                     additionalSpacing: 10.0, state: .normal)
            topView.addSubview(btn)
            startX += 10 + (KScreenWidth-60)/5
            if (index == 4) {
                startX = 10
                startY = (KScreenWidth-60)/5 + 20
            }
        }

    }
    
    fileprivate func setupContentView(){
        let label = UILabel()
        label.frame = CGRect(x: (KScreenWidth - 100)/2, y: ((KScreenWidth-60)/5 + 20)*2+170, width: 100, height: 20)
        label.text = "个性化推荐"
        label.font = UIFont.init(name: "Helvetica-Bold", size: 17)
        label.textAlignment = .center
        label.textColor = UIColor.black
        addSubview(label)
        
        let la = UILabel()
        la.frame = CGRect(x: KScreenWidth-60, y: ((KScreenWidth-60)/5 + 20)*2+175, width: 25, height: 15)
        la.text = "更多"
        la.font = UIFont.systemFont(ofSize: 12)
        la.textAlignment = .right
        la.textColor = UIColor.lightGray
        addSubview(la)
        
        let img2 = UIImageView()
        img2.frame = CGRect(x: KScreenWidth-35, y: ((KScreenWidth-60)/5 + 20)*2+175, width: 20, height: 15)
        img2.image = UIImage(named:"arrow")
        addSubview(img2)
        
        let btn = UIButton()
        btn.frame = CGRect(x: KScreenWidth-75, y: ((KScreenWidth-60)/5 + 20)*2+170, width: 60, height: 20)
        addSubview(btn)
        
        let h = KScreenWidth - 50
        //定义collectionView的布局类型，流布局
        let layout = UICollectionViewFlowLayout()
        //设置cell的大小
        layout.itemSize = CGSize(width: (KScreenWidth-40)/3, height: (KScreenWidth-40)/3+40)
//        //滑动方向 默认方向是垂直
//        layout.scrollDirection = .vertical
        //每个Item之间最小的间距
        layout.minimumInteritemSpacing = 0
        //每行之间最小的间距
        layout.minimumLineSpacing = 0
        //定义一个UICollectionView
        let collectionView = UICollectionView(frame: CGRect(x: 0,y: ((KScreenWidth-60)/5 + 20)*2+200,width: KScreenWidth,height: h), collectionViewLayout: layout)
        //设置collectionView的背景颜色
        collectionView.backgroundColor = UIColor.white
        addSubview(collectionView)
        //设置collectionView的代理和数据源
        collectionView.isScrollEnabled = false
        collectionView.delegate = self
        collectionView.dataSource = self
        //CollectionViewCell的注册
        collectionView.register(UINib(nibName: "LMJCollectionViewCell", bundle: nil), forCellWithReuseIdentifier: "MyCell")

    }
}

extension MusicStoreView: UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout{
    // #MARK: --UICollectionViewDataSource的代理方法
    /**
     - 该方法是可选方法，默认为1
     - returns: CollectionView中section的个数
     */
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1
    }
    
    /**
     - returns: Section中Item的个数
     */
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return images.count
    }
    
    /**
     - returns: 绘制collectionView的cell
     */
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "MyCell", for: indexPath) as! LMJCollectionViewCell
        
        cell.imageView.image = UIImage(named: images[indexPath.item])
        cell.title.text = titleArr[indexPath.item]
        
        return cell
    }
    
    // #MARK: --UICollectionViewDelegate的代理方法
    /**
     Description:当点击某个Item之后的回应
     */
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        print("(\(indexPath.section),\(indexPath.row))")
    }
    
    /**
     Description:可以定制不同的item
     
     - returns: item的大小
     */
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        return CGSize(width: (KScreenWidth-40)/3, height: (KScreenWidth-40)/3+40)
    }
    
    //返回cell 上下左右的间距
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets{
        return UIEdgeInsetsMake(10, 10, 10, 10)
    }
}





